<style scoped>
  .weui-form-preview__hd .weui-form-preview__value {
    font-size: 1.2em;
    padding-right: 20px;
  }
  .page-infinite-wrapper {
    background: #e3e3e3;
  }
  .sellform >>> .weui-cells {
    margin-top: 0;
    background: #e3e3e3;
  }
  .sellform >>> .weui-loadmore__tips {
    background: #e3e3e3;;
  }
  .sellform >>> .weui-loadmore {
    margin-bottom: 0;
  }
  .weui-form-preview {
    margin: 5px;
    border-radius: 5px;
  }

  .rj-cell-child {
    display: inline-block;
    font-size: 0;
    padding: 5px 15px;
    text-align: left;
  }
  .rj-cell-child.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell-child.minpad{
    padding-right: 10px !important;
  }
  .rj-cell-child label {
    color: #333;
    font-size: 14px;
  }
  .rj-cell-child span {
    padding-left: 20px;
    font-size: 14px;
    color: #333;
  }
  .rj-cell-child .tip {
    font-size: 16px;
    color: #333;
  }
  .rj-cell-child i {
    font-size: 18px;
    font-style: normal;
    color: #333;
  }
  .rj-cell-child-tit {
    background: #eee;
    font-size: 14px;
    padding: 5px 15px;
    border-bottom: 1px solid #ddd;
  }
  .weui-form-preview__ft .weui-form-preview {
    background: #f5f5ef;
  }
  .weui-form-preview__ft {
    line-height: normal;
  }



  .weui_tab_bd {
    background: #eee;
  }
  .weui-form-preview {
    background-color: #fff;
    margin: 5px;
    border-radius: 5px;
  }
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui_navbar {
    white-space: nowrap;
    overflow: scroll;
    display: block !important;
    border-bottom: 1px solid #BCBAB6;
    font-size: 0;
    /*background: #fafafa;*/
  }
  .weui_navbar_item {
    font-size: 14px;
    display: inline-block;
    width: 33.3%;
    padding: 13px 0;
    background: #fff;
    position: relative;
    color: #888;
  }
  .weui_navbar_item.weui_bar_item_on {
    background-color: #f5f5f5;
    color: #00913a;
  }
  .weui_navbar:after {
    border-bottom: 0;
  }
  .weui_navbar_item:after {
    content: " ";
    position: absolute;
    right: 0;
    top: 0;
    width: 1px;
    height: 100%;
    border-right: 1px solid #cccccc;
    color: #cccccc;
    -webkit-transform-origin: 100% 0;
    transform-origin: 100% 0;
    -webkit-transform: scaleX(0.5);
    transform: scaleX(0.5);
    font-size: 10px;
  }
  .weui-panel__bd {
    text-align: left;
  }
  .hoverbar {
    background: #03A9F4;
    position: fixed;
    bottom: 100px;
    right: 20px;
    height: 50px;
    line-height: 50px;
    width: 50px;
    z-index: 100;
    font-size: 30px;
    border-radius: 50%;
    box-shadow: 0px 2px 3px rgba(100, 100, 100, 0.5);
    color: #fff;
  }
  .flowbtn {
    position: absolute;
    top: 5px;
    right: 5px;
    z-index: 1000;
  }
  .subscript{
    color: #fff;
    height: 15px;
    width: 80px;
    position: absolute;
    right: -27px;
    top:5px;
    text-align: center;
    line-height: 15px;
    font-family: "黑体";
    background-color: #FF0000;
    -moz-transform:rotate(45deg);
    -webkit-transform:rotate(45deg);
    -o-transform:rotate(45deg);
    -ms-transform:rotate(45deg);
    transform:rotate(45deg);
  }
  .delsty {
    position: absolute;
    height: 20px;
    width: 20px;
    z-index: 100;
    border-radius: 50%;
    box-shadow: 0 2px 3px hsla(0,0%,39.2%,.5);
    text-align: center;
    right: 5px;
    top: 5px;
    color: red;
    font-size: 14px;
  }
</style>

<template>
  <div class="pickinglist">
    <div class="hoverbar" v-if="addPerm"  @click="addItem">+</div>
    <SearchBar @doSearch="doSearch" />
    <div class="weui_tab">
      <div class="weui_navbar order-tabhd">
<!--        <a class="weui_navbar_item" v-for="(item, index) in tabTitles" :key="item.guid" :style="'width:' + tabWidth" :class="{ weui_bar_item_on: postParams.status == item.stats }" href="javaScript:;" @click="switchStatus(item.stats)">{{item.name}}</a>-->
        <a class="weui_navbar_item" :class="{ weui_bar_item_on: postParams.stats == 3 }" href="javaScript:;" @click="switchStatus(3)">生产中</a>
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: postParams.stats == 4 }" href="javaScript:;" @click="switchStatus(4)">已结束</a>
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: postParams.stats == 5 }" href="javaScript:;" @click="switchStatus(5)">已同步</a>
      </div>
      <div class="weui_tab_bd" style="background-color: #fff">
        <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }" >
          <mescroll-vue ref="mescroll" id="wrapper" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
            <div class="weui-form-preview" v-for="(item, index) in data" :key="item.guid" :ref="item.guid" style="position: relative;">
<!--              <div class="delsty"   v-show="item.createBy == userid"  @click="delItem(item.id)">×</div>-->
              <div class="weui-form-preview__hd" @click="viewItem(item.guid)">
                <label class="weui-form-preview__label">编号</label>
                <em class="weui-form-preview__value">{{item.code}}</em>
              </div>
              <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">计划名称</label>
                  <span class="weui-form-preview__value">{{item.name}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">类型</label>
                  <span class="weui-form-preview__value">{{item.typeName}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">出库库房</label>
                  <span class="weui-form-preview__value">{{item.strname}}</span>
                </div>
<!--                <div class="weui-form-preview__item">-->
<!--                  <label class="weui-form-preview__label">总重量</label>-->
<!--                  <span class="weui-form-preview__value">{{item.weight ? `${item.weight} 吨` : ''}}</span>-->
<!--                </div>-->
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">生产日期</label>
                  <span class="weui-form-preview__value">{{item.planstart}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">状态</label>
                  <span class="weui-form-preview__value">{{item.statusName}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">备注</label>
                  <span class="weui-form-preview__value">{{item.remarks}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">提交人</label>
                  <span class="weui-form-preview__value">{{item.createName}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">创建时间</label>
                  <span class="weui-form-preview__value">{{item.createtime}}</span>
                </div>
              </div>
              <div class="weui-form-preview__ft" style="line-height: 50px;" >
                <template v-for="(permission, index) in permissions">
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" style="overflow: hidden;" href="javascript:" v-show="permission != undefined && permission.functiontype.indexOf(3) >= 0 && permission.scopeType ==3  && item.status == 3 && permission.srguid == item.strmid  && permission.permission == 16" @click="doActAl('AddPickmrial2', item.guid,item.pickinglistCount)" >生产领料 <div class="subscript">{{item.pickinglistCount}}</div></a>
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="permission != undefined && permission.functiontype.indexOf(3) >= 0 && permission.scopeType ==7  && item.status == 3 && permission.srguid == item.strmid  && permission.permission == 17" @click="finish(item.guid)" >完成</a>
                  <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="permission != undefined && permission.functiontype.indexOf(3) >= 0 && permission.scopeType ==7  && item.status == 4 && permission.srguid == item.strmid  && permission.permission == 17" @click="reset(item.guid)" >重新启动</a>
  <!--                <a class="weui-form-preview__btn weui-form-preview__btn_primary" v-for="(node, index) in item.nodes" v-if="node.operate != 'start'" :key="node.guid" href="javascript:" @click="doAct(node.operate, item.id, node.flowTaskId, node.name, node.annex, node.refuseflag)" >{{node.name}}</a>-->
                </template>
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="item.createBy == userid"  @click="delItem(item.guid)" >删除</a>
              </div>
            </div>
          </mescroll-vue>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/babel">
  import MescrollVue from 'mescroll.js/mescroll.vue'
  import SearchBar from '../common/SearchBar'
  export default {
    name: 'PlanList2',
    components: {SearchBar, MescrollVue},
    data() {
      return {
        flowid: 30,
        userid: localStorage.userId,
        permission: {},
        permissions: [],
        isShow:true,
        postParams: {
          stats: 3
        },
        mescroll: null, // mescroll实例对象
        mescrollDown: {},
        mescrollUp: { // 上拉加载的配置.
          callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
          page: {
            num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
            size: 5 //每页数据条数,默认10
          },
          htmlNodata: '<p class="upwarp-nodata">-- 我是有底线的 --</p>',
          noMoreSize: 1,
          empty: {
            warpId: "wrapper", //父布局的id (1.3.5版本支持传入dom元素)
            tip: "暂无相关数据~" //提示
          }
        },
        wrapperHeight: 700,
        data: [],
        tabTitles: [],
        tabWidth: '50%',
        addPerm: ''
      }
    },
    created() {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";
    },
    async mounted() {
      if (this.$route.query.code != undefined) {
        this.postParams.search = this.$route.query.code;
      }
      if (this.$route.query.status != undefined) {
        this.postParams.stats = this.$route.query.status;
      }
      if (this.$route.query.token != undefined) {
        localStorage.setItem('token', this.$route.query.token);
      }
      // await this.getLabels();
      setTimeout(() => {
        this.wrapperHeight = (document.body.scrollHeight || document.documentElement.scrollHeight) - this.$refs.wrapper.offsetTop;
      }, 1000)

      //获取权限
      this.getPermission();
    },
    methods: {
      delItem: function (guid) {
        var that = this;
        $.confirm("确定删除!", function() {
          that.axios({
            method: 'post',
            url: `/prod-api/produce/plan/${guid}`,
          }).then((response) => {
            if (response.data.code == 200) {
              // that.$refs[guid][0].remove();
              that.getPageData();
              that.toastSuccess('删除成功！');
            } else if (response.data.msg) {
              that.toastText(response.data.msg);
            } else {
              that.toastSuccess('删除失败！');
            }

          })
        }, function() {
          //点击取消后的回调函数
        });
      },
      addItem: function() {
        this.$router.push({name: 'AddPlan2'});
      },
      getLabels: async function() {
        await this.axios({
          method: 'get',
          params:{flowId: this.flowid},
          url: '/prod-api/getTabsByFlowId',
        }).then((response) => {
          if (response.data.code == 200) {
            this.$nextTick(() => {
              this.tabTitles = response.data.data;
              //tab最大宽度20%，默认选中第一个tab页
              this.tabWidth = response.data.data.length < 6 ? (100 / response.data.data.length) + '%' : '20%';
              console.log('this.tabWidth:', this.tabWidth)
              if (this.$route.query.label != undefined) {
                this.postParams.stats = this.tabTitles[this.currTab].stats;
                this.getPageData();
              } else if (this.postParams.stats != this.tabTitles[0].stats) {
                this.postParams.stats = this.tabTitles[0].stats;
                this.getPageData();
              } else {
                this.postParams.stats = this.tabTitles[0].stats;
              }
            })
          } else {
            this.toastError("系统错误");
          }
        })
      },
      getPermission: async function() {
        await this.axios({
          method: 'get',
          url: '/prod-api/getPermis',
        }).then((response) => {
          if (response.data.code == 200) {
            this.permissions = response.data.data;
            this.addPerm = response.data.data.some(item => item.permission == 17);
          } else {
            this.toastError("获取用户权限失败");
          }

        })
      },
      switchStatus: function (sta) {
        this.postParams.stats = sta;
        this.getPageData();
      },
      mescrollInit (mescroll) {
        this.mescroll = mescroll  // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
      },
      upCallback (page, mescroll) {
        //alert(this.postParams.code)
        this.postParams.pageNum = page.num;
        this.postParams.pageSize = page.size;
        this.postParams.functionType = 3;
        // 联网请求
        this.axios({
          method: 'get',
          url: '/prod-api/produce/plan/list',
          params: this.postParams
        }).then((response) => {
          console.log('response:', response)
          if (response.data.code == 200) {
            let data = response.data.rows != undefined ? response.data.rows : [];

            // 如果是第一页需手动置空列表
            if (page.num === 1 && this.data.length != 0) this.data = [];
            // 把请求到的数据添加到列表
            this.data = this.data.concat(data);
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(data.length)
            })
            //将获取上一访问列表临时页码恢复正常
            if (this.mescrollUp.page.size > 5) {
              this.mescrollUp.page.num = this.$store.state.list.pageNo;
              this.mescrollUp.page.size = 5;
            }
          } else if (response.data.msg) {
            this.toastText(response.data.msg);
          } else {
            this.toastError("系统错误");
          }

        }).catch((e) => {
          // 联网失败的回调,隐藏下拉刷新和上拉加载的状态;
          mescroll.endErr()
        })
      },
      getPageData: function () {
        $('#wrapper').scrollTop(0);
        this.data = [];
        this.mescroll.resetUpScroll();
      },
      doSearch: function(keywords) {
        this.postParams.search = keywords;
        this.getPageData();
      },
      viewItem: function(id) {
        this.$router.push({name: 'ViewPlan2', query: {id:id}});
      },
      finish: async function(id) {
        let that = this;
        let contnu = 0;
        let confirmFlag;
        await that.axios({
          method: 'post',
          params:{planid: id},
          url: '/prod-api/produce/plan/finish',
        }).then((response) => {
          if (response.data.code == 200) {
            contnu = 1;
            confirmFlag = response.data.confirmFlag;
          } else if (response.data.msg == 'outWeight') {
            contnu = 2;
            that.losspoint = response.data.lossPoint;
            confirmFlag = response.data.confirmFlag;
          } else if (response.data.msg == 'noPick') {
            that.toastText('没有领料单，不允许结束');
          } else if (response.data.msg == 'hasNoWhoutPick') {
            that.toastText('有尚未出库的领料单，不允许结束');
          } else if (response.data.msg == 'hasNoWhinApply') {
            that.toastText('没有产品入库单或产品入库单未完成，不允许结束');
          } else if (response.data.msg == 'hasNoWhinPick') {
            that.toastText('有尚未入库的产品入库单，不允许结束');
          } else if (response.data.msg == 'hasNoMotPick') {
            that.toastText('有尚未完成的退料单，不允许结束');
          } else if (response.data.msg == 'scrap') {
            that.toastText('有未出库的报废申请,不能完成');
          } else if (response.data.msg == 'whin') {
            that.toastText("有未入库的入库申请单,不能完成");
          } else if (response.data.msg == 'materialret') {
            that.toastText("有未入库的退料申请单,不能完成");
          } else if (response.data.msg == 'unwhout') {
            that.toastText("有未出库的领料单,不能完成");
          } else if (response.data.msg == 'noWhinMater') {
            that.toastText("有尚未完成的领料单,不能完成");
          } else if (response.data.msg == 'outButNotIn') {
            that.toastText("有领料单未做产品入库,不能完成");
          } else {
            this.toastError("系统错误");
          }
        })
        if (contnu == 0) return;
        let confirmtxt = contnu == 2 ? `该生产计划损耗超过 ${that.losspoint}%，请确认所有数据已录入` : '确定要执行此操作？';
        $.confirm(confirmtxt, function () {
          that.axios({
            method: 'post',
            params:{planid: id, confirmFlag: confirmFlag},
            url: '/prod-api/produce/plan/finish',
          }).then((response) => {
            if (response.data.code == 200) {
              that.toastSuccess('操作成功');
              that.getPageData();
            } else if (response.data.msg == 'noPick') {
              that.toastText('没有领料单的，不允许结束');
            } else if (response.data.msg == 'hasNoWhoutPick') {
              that.toastText('有尚未出库的领料单，不允许结束');
            } else if (response.data.msg == 'hasNoWhinApply') {
              that.toastText('没有产品入库单或产品入库单未完成，不允许结束');
            } else if (response.data.msg == 'hasNoWhinPick') {
              that.toastText('有尚未入库的产品入库单，不允许结束');
            } else if (response.data.msg == 'hasNoMotPick') {
              that.toastText('有尚未完成的退料单，不允许结束');
            } else if (response.data.msg == 'scrap') {
              that.toastText('有未出库的报废申请,不能完成');
            } else if (response.data.msg == 'whin') {
              that.toastText("有未入库的入库申请单,不能完成");
            } else if (response.data.msg == 'materialret') {
              that.toastText("有未入库的退料申请单,不能完成");
            } else if (response.data.msg == 'unwhout') {
              that.toastText("有未出库的领料单,不能完成");
            } else if (response.data.msg == 'noWhinMater') {
              that.toastText("有尚未完成的领料单,不能完成");
            } else {
              this.toastError("系统错误");
            }
          })
        })
      },
      reset: function(id) {
        let that = this;
        $.confirm('确定要执行此操作？', function () {
          that.axios({
            method: 'get',
            // params:{planid: id},
            url: `/prod-api/produce/plan/enable/${id}`,
          }).then((response) => {
            if (response.data.code == 200) {
              that.toastSuccess('操作成功');
              that.getPageData();
            } else if (response.data.msg) {
              that.toastText(response.data.msg);
            } else {
              that.toastError("系统错误");
            }
          })
        })
      },
      // doAct: async function (name, id, nodeid, actname, annex, refuseflag) {
      //   let that = this;
      //   let acts = this.$store.state.acts;
      //   that.$router.push({name: acts[name], query: {id:id, nodeid: nodeid, title: actname, annex: annex, refuseflag: refuseflag}});
      // },
      doAct: function (name, guid, type = 0) {
        if (type == 0) {
          this.$router.push({name: name, query: {guid:guid, conditionSR: this.conditionSR}});
        } else {
          this.$router.push({name: name, query: {guid:guid, type: type, conditionSR: this.conditionSR}});
        }
      },
      doActAl: function (name, guid, count) {
        var that = this;
        if(count != 0){

          $.confirm("该生产计划已经存在" + count + "张领料单，请确认是否继续录入", function() {
            that.$router.push({name: name, query: {guid:guid}});
          }, function() {
            //点击取消后的回调函数
          });
        }else{
          that.$router.push({name: name, query: {guid:guid}});
        }
      },
    },
    activated () {
      //列表页头部不允许滑动
      document.getElementsByTagName("body")[0].className="bg";


      //判断是否是返回上一页
      if (this.$store.state.list.reffer != this.$options.name) return;
      //获取上一次访问列表数据
      this.mescrollUp.page.num = 0;
      this.mescrollUp.page.size = this.$store.state.list.pageNo * this.mescrollUp.page.size;
      this.getPageData();
      //定位
      this.$nextTick(function(){
        let position = this.$store.state.list.postion //返回页面取出来
        $('#wrapper').scrollTop(position);
        let status = this.$store.state.list.status;
        this.postParams.stats = status;
      })
    },
    beforeRouteLeave(to, from, next){
      //取消列表页头部不允许滑动
      document.body.removeAttribute("class","bg");

      let position = $('#wrapper').scrollTop();
      let status = this.postParams.stats;
      this.$store.commit('setPosition', position) //离开路由时把位置存起来
      this.$store.commit('setStatus', status)
      this.$store.commit('setPageNo', this.mescrollUp.page.num);
      this.$store.commit('setReffer', this.$options.name);
      next()
    },



  };
</script>
